package test_0926;

import java.util.Arrays;

public class TimeComplexity {
    public static void main(String[] args) {
        long[]array={1,3,2,5,7,9};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
        System.out.println(binarySearch(array,4));
        System.out.println(binarySearch(array,9));
    }
    public static void bubbleSort(long []array){
        for(int i=0;i< array.length;i++){
            for(int j=i;j< array.length-i-1;j++){
                if(array[j]>array[j+1]){
                    long temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
        }
    }
    public static int binarySearch(long[]array,long target){
        int begin=0;
        int last=array.length;//左闭右开！！！！！
        while(begin<last) {
            int mid=begin+(last-begin)/2;//偶数区间是指向左边的
            if (array[mid] < target) {
                begin=mid+1;
            } else if (array[mid] > target) {
                last=mid;
            } else {
                return mid;
            }
        }
        return -1;//没找到
    }

    public static int binarySearch1(long[]array,long target){
        int begin=-1;
        int last=array.length-1;//左开右闭！！！！！
        while(begin<last) {
            int mid=begin+(last-begin+1);//偶数区间是指向右边的
            if (array[mid] < target) {
                begin=mid;
            } else if (array[mid] > target) {
                last=mid-1;
            } else {
                return mid;
            }
        }
        return -1;//没找到
    }
    public static int binarySearch2(long[]array,long target){
        int begin=0;
        int last=array.length-1;//左闭右闭！！！
        while(begin<=last) {
            int mid=begin+(last-begin)/2;//偶数区间是指向左边的
            if (array[mid] < target) {
                begin=mid+1;
            } else if (array[mid] > target) {
                last=mid-1;
            } else {
                return mid;
            }
        }
        return -1;//没找到
    }
}


